package com.action;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.bean.Course;
import com.bean.Question;
import com.bean.Student;
import com.dao.CourseDao;
import com.dao.QuestionDao;
import com.opensymphony.xwork2.ActionSupport;

public class QuestionAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7143807218653450713L;
	
	private List<Course> courseList;
	
	private List<Question> questionList;
	
	private String courseID;
	
	private String quesContent;
	
	private String ansPerson;
	
	private String answer;
	
	private String questionID;
	
	CourseDao cd = new CourseDao();
	
	QuestionDao qd = new QuestionDao();
	
	public String getList(){
		courseList = cd.getCourseList();
		questionList = qd.getQuestionList();
		return SUCCESS;
	}
	//问问题
	public String askQuestion(){
		courseList = cd.getCourseList();
		Student stu = new Student();
		stu = (Student) ServletActionContext.getRequest().getSession().getAttribute("stu");
		Date d = new Date();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		try {
			quesContent = new String(quesContent.getBytes("iso-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		Question question = new Question();
		question.setCourseID(Integer.parseInt(courseID));
		question.setAskPerson(String.valueOf(stu.getStuUserID()));
		question.setAnsPerson(String.valueOf(ansPerson));
		question.setQuesContent(quesContent);
		question.setCreateTime(df.format(d));
		qd.add(question);
		questionList = qd.getQuestionList();
		return SUCCESS;
	}

	//回答问题
	public String ansQuestion(){
		Date d = new Date();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		try {
			answer  = new String(answer.getBytes("iso-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Question question = new Question();
		question.setQuestionID(Integer.parseInt(questionID));
		question.setAnswer(answer);
		question.setAnswerTime(df.format(d));
		qd.answerQuestion(question);
		questionList = qd.getQuestionList();
		//System.out.println(answer+"---------"+questionID);
		return SUCCESS;
	}
	public void setCourseList(List<Course> courseList) {
		this.courseList = courseList;
	}

	public List<Course> getCourseList() {
		return courseList;
	}

	public String getCourseID() {
		return courseID;
	}

	public void setCourseID(String courseID) {
		this.courseID = courseID;
	}

	public String getQuesContent() {
		return quesContent;
	}

	public void setQuesContent(String quesContent) {
		this.quesContent = quesContent;
	}

	public void setQuestionList(List<Question> questionList) {
		this.questionList = questionList;
	}

	public List<Question> getQuestionList() {
		return questionList;
	}
	public String getAnswer() {
		return answer;
	}
	public void setAnswer(String answer) {
		this.answer = answer;
	}
	public String getQuestionID() {
		return questionID;
	}
	public void setQuestionID(String questionID) {
		this.questionID = questionID;
	}
	public String getAnsPerson() {
		return ansPerson;
	}
	public void setAnsPerson(String ansPerson) {
		this.ansPerson = ansPerson;
	}

}
